< previous page page_214 next page >

Page 214
LoadImage returns a handle to the newly loaded image when it succeeds. To display the image, you must create a device context and use the image's handle to copy it into the device context. Then you can use the BitBlt API function to copy the result into a picture box. Finally, you should destroy the device context and image to free their graphic resources.
The following code shows how a program can use LoadImage to load a picture at a desired size.
e10a6a0a59f035e870d90cca05b85fce.gif
Private Declare Function LoadImage Lib "user32" Alias _
    "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, _
    ByVal unl As Long, ByVal nl As Long, ByVal n2 As Long, _
    ByVal un2 As Long) As Long
Private Const LR_LOADFROMFILE = &H10
Private Declare Function CreateCompatibleDC Lib "gdi32" ( _
    ByVal hdc As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" ( _
    ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" ( _
    ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" ( _
    ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "gdi32" ( _
    ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, _
    ByVal nWidth As Long, ByVal nHeight As Long, _
    ByVal hSrcDC As Long, ByVal xSrc As Long, _
    ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Const SRCCOPY = &HCC0020

' Use the LoadImage API function to load a picture
' from a file into a PictureBox, filling the PictureBox.
Private Sub LoadImageFromFile(ByVal file_name As String, _
    ByVal pic As PictureBox)
Dim wid As Long
Dim hgt As Long
Dim mem_dc As Long
Dim hbmp As Long

    ' Get the desired size in pixels.
    wid = pic.ScaleX(pic.ScaleWidth, pic.ScaleMode, vbPixels)
    hgt = pic.ScaleY(pic.ScaleHeight, pic.ScaleMode, vbPixels)

    ' Get the bitmap handle from the file.
    hbmp = LoadImage(ByVal 0&, file_name, 0, _
        wid, hgt, LR_LOADFROMFILE)

     ' Create a device context to hold the image.
     mem_dc = CreateCompatibleDC(0)

     ' Select the bitmap into the device context.
     SelectObject mem_dc, hbmp

 
< previous page page_214 next page >